
function getGrade() {
    let select_grade = $("#grade");
    $.ajax({
        type: "GET",
        url: "webapi/student/getAllGrade",
    }).done(function (allGrade){
        for (let key in allGrade) {
            let grade = $("<option>" + allGrade[key] + "</option>");
            select_grade.append(grade);
        }
    });
}

function getClazz(){
    let select_clazz = $("#clazz");
    $.ajax({
        type: "GET",
        url: "webapi/student/getAllClazz",
    }).done(function (allClazz){
        for (let key in allClazz) {
            let clazz = $("<option>" + allClazz[key] + "</option>");
            select_clazz.append(clazz);
        }
    });
}

function loadTable(page) {
    let sex = $("#sex").val();
    let grade = $("#grade").val();
    let clazz = $("#clazz").val();
    let num = $("#num").val().trim();
    let name = $("#name").val().trim();
    let tid = $("#tid").val();

    $.ajax({
        type: "GET",
        url: "webapi/student/getByPage",
        data: {
            sex: sex,
            grade: grade,
            clazz: clazz,
            num: num,
            name: name,
            tid: tid,
            page: page
        }
    }).done(function (pageUtil) {
        //console.log(pageUtil);
        let table = $("#stuTable");
        let pagination = $("#pagination");
        //先清空表格
        table.empty();
        pagination.empty();

        let list = pageUtil.list;
        //插入学生信息
        for (let key in list) {
            let student = list[key];
            let id = $("<td>" + student.id + "</td>");
            let num = $("<td>" + student.num + "</td>");
            let name = $("<td>" + student.name + "</td>");
            let sexStr;
            if (student.sex === 0){
                sexStr = "男";
            } else if (student.sex === 1){
                sexStr = "女";
            }
            let sex = $("<td>" + sexStr + "</td>");
            let grade = $("<td>" + student.grade + "</td>");
            let clazz = $("<td>" + student.clazz + "</td>");
            let button = $("<td></td>")
                .append($("<a class='button primary' href='/toUpdateStudent?id=" + student.id + "'>编辑</a>"))
                .append($("<a class='button' onclick='deleteById(" + student.id + ")'>删除</a>"))

            let stuInfo = $("<tr></tr>").append(id).append(num).append(name).append(sex).append(grade).append(clazz).append(button);
            table.append(stuInfo);
        }
        //页码处理
        let current = pageUtil.current;
        let totalPage = pageUtil.totalPage;
        let total = pageUtil.total;
        pagination.append("<li><span class='button small' onclick='loadTable(0)'>首页</span></li>")
        if (current > 0){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current - 1) + ")'>上一页</span></li>");
        }
        for (let i = 0; i < totalPage; i++) {
            let pageNum = $("<li><span class='page' onclick='loadTable(" + i + ")'>" + (i + 1) + "</span></li>");
            if (i === current){
                pageNum.children("span").addClass("active");
            }
            pagination.append(pageNum);
        }
        if (current < totalPage - 1){
            pagination.append("<li><span class='button small' onclick='loadTable(" + (current + 1) + ")'>下一页</span></li>");
        }
        pagination.append("<li><span class='button small' onclick='loadTable(" + (totalPage - 1) + ")'>尾页</span></li>");
        pagination.append("<li>共有" + total + "名学生" + "</li>");
    });
}

function reset() {
    let sex = $("#sex");
    let grade = $("#grade");
    let clazz = $("#clazz");
    let num = $("#num");
    let name = $("#name");

    sex.val("");
    grade.val("");
    clazz.val("");
    num.val("");
    name.val("");
}

function deleteById(id) {
    if (confirm("确认删除该学生信息？")){
        $.ajax({
            type: "DELETE",
            url: "webapi/student/deleteById",
            data: {
                id: id
            }
        }).done(function (data){
            if (data === 1){
                alert("删除成功");
                loadTable(0);
            } else {
                alert("删除失败");
            }
        });
    }
}

/**
 * 获取用户选择的文件名
 */
function getSelectFilename(){
    let file = $("#file").val();
    $("#uploadFilename").val(file);
}

function addStu() {
    let num = $("#num").val();
    let name = $("#name").val();
    let sex = $("#sex").val();
    let pwd = $("#pwd").val();
    let grade = $("#grade").val();
    let clazz = $("#clazz").val();
    let tid = $("#tid").val();

    $.ajax({
        type: "POST",
        url: "webapi/student/saveStudent",
        data: {
            num: num,
            name: name,
            sex: sex,
            pwd: pwd,
            grade: grade,
            clazz: clazz,
            tid: tid
        }
    }).done(function (data) {
        if (data === 1){
            alert("添加成功");
            history.back();
        } else if (data === 0){
            alert("该学生信息已存在");
        } else if (data === -1){
            alert("添加失败");
        }
    });
}

/**
 * 上传文件
 */
function addStuByFile() {
    let files = $("#file").prop("files");
    let tid = $("#tid").val();
    //console.log(files);
    let data = new FormData;
    data.append("file", files[0]);
    data.append("tid", tid);
    $.ajax({
        type: "POST",
        url: "/webapi/student/addStudentByFile",
        data: data,
        processData: false, //无须序列化
        contentType: false, //无须处理
        cache: false //无须缓存
    }).done(function (data){
        if (data > 0){
            alert("添加成功，新增" + data + "条数据");
            window.location.href = "/toManageExercise";
        } else if (data === -1){
            alert("添加失败");
        }
    });
}

function updateStu() {
    let id = $("#id").val();
    let num = $("#num").val();
    let name = $("#name").val();
    let sex = $("#sex").val();
    let pwd = $("#pwd").val();
    let grade = $("#grade").val();
    let clazz = $("#clazz").val();
    let tid = $("#tid").val();

    $.ajax({
        type: "POST",
        url: "webapi/student/saveStudent",
        data: {
            id: id,
            num: num,
            name: name,
            sex: sex,
            pwd: pwd,
            grade: grade,
            clazz: clazz,
            tid: tid,
            isAdd: false
        }
    }).done(function (data) {
        if (data === 1){
            alert("更新成功");
            window.location.href = "/toManageStu";
        } else if (data === -1){
            alert("更新失败");
        }
    });
}